home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dummy_thread.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Drop-in replacement for the thread module.
  5.  
  6. Meant to be used as a brain-dead substitute so that threaded code does
  7. not need to be rewritten for when the thread module is not present.
  8.  
  9. Suggested usage is::
  10.  
  11.     try:
  12.         import thread
  13.     except ImportError:
  14.         import dummy_thread as thread
  15.  
  16. '''
  17. __all__ = [
  18.     'error',
  19.     'start_new_thread',
  20.     'exit',
  21.     'get_ident',
  22.     'allocate_lock',
  23.     'interrupt_main',
  24.     'LockType']
  25. import traceback as _traceback
  26.  
  27. class error(Exception):
  28.     '''Dummy implementation of thread.error.'''
  29.     
  30.     def __init__(self, *args):
  31.         self.args = args
  32.  
  33.  
  34.  
  35. def start_new_thread(function, args, kwargs = { }):
  36.     '''Dummy implementation of thread.start_new_thread().
  37.  
  38.     Compatibility is maintained by making sure that ``args`` is a
  39.     tuple and ``kwargs`` is a dictionary.  If an exception is raised
  40.     and it is SystemExit (which can be done by thread.exit()) it is
  41.     caught and nothing is done; all other exceptions are printed out
  42.     by using traceback.print_exc().
  43.  
  44.     If the executed function calls interrupt_main the KeyboardInterrupt will be
  45.     raised when the function returns.
  46.  
  47.     '''
  48.     global _main, _interrupt
  49.     if type(args) != type(tuple()):
  50.         raise TypeError('2nd arg must be a tuple')
  51.     type(args) != type(tuple())
  52.     if type(kwargs) != type(dict()):
  53.         raise TypeError('3rd arg must be a dict')
  54.     type(kwargs) != type(dict())
  55.     _main = False
  56.     
  57.     try:
  58.         function(*args, **kwargs)
  59.     except SystemExit:
  60.         pass
  61.     except:
  62.         _traceback.print_exc()
  63.  
  64.     _main = True
  65.     if _interrupt:
  66.         _interrupt = False
  67.         raise KeyboardInterrupt
  68.     _interrupt
  69.  
  70.  
  71. def exit():
  72.     '''Dummy implementation of thread.exit().'''
  73.     raise SystemExit
  74.  
  75.  
  76. def get_ident():
  77.     '''Dummy implementation of thread.get_ident().
  78.  
  79.     Since this module should only be used when threadmodule is not
  80.     available, it is safe to assume that the current process is the
  81.     only thread.  Thus a constant can be safely returned.
  82.     '''
  83.     return -1
  84.  
  85.  
  86. def allocate_lock():
  87.     '''Dummy implementation of thread.allocate_lock().'''
  88.     return LockType()
  89.  
  90.  
  91. def stack_size(size = None):
  92.     '''Dummy implementation of thread.stack_size().'''
  93.     if size is not None:
  94.         raise error('setting thread stack size not supported')
  95.     size is not None
  96.     return 0
  97.  
  98.  
  99. class LockType(object):
  100.     '''Class implementing dummy implementation of thread.LockType.
  101.  
  102.     Compatibility is maintained by maintaining self.locked_status
  103.     which is a boolean that stores the state of the lock.  Pickling of
  104.     the lock, though, should not be done since if the thread module is
  105.     then used with an unpickled ``lock()`` from here problems could
  106.     occur from this class not having atomic methods.
  107.  
  108.     '''
  109.     
  110.     def __init__(self):
  111.         self.locked_status = False
  112.  
  113.     
  114.     def acquire(self, waitflag = None):
  115.         """Dummy implementation of acquire().
  116.  
  117.         For blocking calls, self.locked_status is automatically set to
  118.         True and returned appropriately based on value of
  119.         ``waitflag``.  If it is non-blocking, then the value is
  120.         actually checked and not set if it is already acquired.  This
  121.         is all done so that threading.Condition's assert statements
  122.         aren't triggered and throw a little fit.
  123.  
  124.         """
  125.         if waitflag is None or waitflag:
  126.             self.locked_status = True
  127.             return True
  128.         if not self.locked_status:
  129.             self.locked_status = True
  130.             return True
  131.         return False
  132.  
  133.     __enter__ = acquire
  134.     
  135.     def __exit__(self, typ, val, tb):
  136.         self.release()
  137.  
  138.     
  139.     def release(self):
  140.         '''Release the dummy lock.'''
  141.         if not self.locked_status:
  142.             raise error
  143.         self.locked_status
  144.         self.locked_status = False
  145.         return True
  146.  
  147.     
  148.     def locked(self):
  149.         return self.locked_status
  150.  
  151.  
  152. _interrupt = False
  153. _main = True
  154.  
  155. def interrupt_main():
  156.     '''Set _interrupt flag to True to have start_new_thread raise
  157.     KeyboardInterrupt upon exiting.'''
  158.     global _interrupt
  159.     if _main:
  160.         raise KeyboardInterrupt
  161.     _main
  162.     _interrupt = True
  163.  
  164.